package com.rxyb.hongyu.service.logic;

import com.alibaba.fastjson.JSONObject;
import com.rxyb.hongyu.constants.HongYuConstants;
import com.rxyb.hongyu.db.po.ThirdHongyuDictionary;
import com.rxyb.hongyu.gateway.dto.HongYuRsp;
import com.rxyb.hongyu.gateway.dto.push.*;
import com.rxyb.hongyu.gateway.enums.RspCode;
import com.rxyb.toolize.VO;
import com.rxyb.toolize.date.DateHandle;
import com.rxyb.toolize.date.DateTime;
import com.rxyb.toolize.date.enums.FormatDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 本地执行逻辑/挡板
 *
 * @author duyh
 * @date 2020/6/9
 */
@Service
public class HongyuLocalLogic {

    //    @Autowired
//    private RedisObjectUtils redisObjectUtils;
    @Autowired
    private HongyuDictionaryLogic hongyuDictionaryLogic;

    /**
     * 将鸿宇响应码改为系统响应
     */
    public RspCode parse(HongYuRsp hongYuRsp) {
        return null;
    }

    /**
     * 新车
     **/
    public HongYuRsp registerCar(F20A01Req f20A01Req) {
        HongYuRsp rsp = new HongYuRsp();
        rsp.setRetcode("1");
        rsp.setRetmessage("成功");
        F20A01Rsp json = new F20A01Rsp();
        json.setHpzl(f20A01Req.getHpzl());
        json.setHphm(f20A01Req.getHphm());
        json.setClsbdh("*" + f20A01Req.getClsbdh());
        json.setZt("A");
        json.setYxqz(DateTime.getDate(FormatDate.FORMAT_DATE_STR));
        rsp.setVehqueryinfo(JSONObject.toJSONString(json));
        return rsp;
    }

    /**
     * 挡板
     **/
    public HongYuRsp f20A01(F20A01Req f20A01Req) {
        String key = f20A01Req.getHpzl() + f20A01Req.getHphm();
        //        redisObjectUtils.get(HongYuConstants.REDIS_GROUP_VEINFO, key);
        HongYuRsp hongYuRsp = new HongYuRsp();
        hongYuRsp.setRetcode(f20A01Req.getRemark());
        if ("$E".equals(f20A01Req.getRemark())) {
            // 查询失败或处理中
            hongYuRsp.setRetmessage("[挡板]查询请求已发送，请确认号牌信息无误后，等待5-10秒再试！");
        } else {
            // 查询成功
            String res = createVeInfo(f20A01Req);
            if (res == null) {
                hongYuRsp.setRetcode(HongYuConstants.FAILED);
                hongYuRsp.setRetmessage("[挡板]请正确输入车辆信息");
            }
            hongYuRsp.setVehqueryinfo(res);
            hongYuRsp.setRetcode(HongYuConstants.SUCCESS);
            hongYuRsp.setRetmessage("[挡板]成功");
            if ("18".equals(f20A01Req.getHpzl()) || "19".equals(f20A01Req.getHpzl()) || "20".equals(f20A01Req.getHpzl()) || "21".equals(f20A01Req.getHpzl()) || "22".equals(f20A01Req.getHpzl()) || "41".equals(f20A01Req.getHpzl()) || "42".equals(f20A01Req.getHpzl()) || "43".equals(f20A01Req.getHpzl())) {
                // 18:试验汽车、19:试验摩托车、20:临时入境汽车、21:临时入境摩托车、22:临时行驶车、41:无号牌、42:假号牌、43:挪用号牌
                // 以上号牌的数据随机变化
            } else {
                //                redisObjectUtils.set(HongYuConstants.REDIS_GROUP_VEINFO, key, hongYuRsp, TimeUnit.HOURS, 24);
            }
        }
        return hongYuRsp;
    }

    /**
     * 教练汽车起流水失败
     **/
    public HongYuRsp f20A02(F20A02Req f20A02Req) {
        HongYuRsp hongYuRsp = new HongYuRsp();
        if (("16").equals(f20A02Req.getHphm())) {
            hongYuRsp.setRetcode(HongYuConstants.FAILED);
            hongYuRsp.setRetmessage("[挡板]不支持该车型起流水");
        } else {
            F20A02Rsp json = new F20A02Rsp();
            json.setCjrq(DateTime.getDate(FormatDate.FORMAT_TIME_STR));
            json.setJyjgbh(f20A02Req.getJyjgbh());
            json.setJyjgmc("挡板检车站");
            json.setJylsh(VO.radomTransNo());
            hongYuRsp.setVehqueryinfo(JSONObject.toJSONString(json));
        }
        return hongYuRsp;
    }

    public HongYuRsp f20A03(F20A03Req f20A03Req) {
        HongYuRsp hongYuRsp = new HongYuRsp();
        // 流水检验结果查询挡板，这个接口不会调用
        hongYuRsp.setRetcode(HongYuConstants.FAILED);
        hongYuRsp.setRetmessage("[挡板]查询失败");
        return hongYuRsp;
    }

    /**
     * 如果车牌号包含Z，则写入失败
     **/
    public HongYuRsp f20A04(F20A04Req f20A04Req) {
        HongYuRsp hongYuRsp = new HongYuRsp();
        if (f20A04Req.getHphm().indexOf("Z") >= 0) {
            hongYuRsp.setRetcode(HongYuConstants.FAILED);
            hongYuRsp.setRetmessage("[挡板]写入失败");
        } else {
            hongYuRsp.setRetcode(HongYuConstants.SUCCESS);
            hongYuRsp.setRetmessage("[挡板]写入成功");
        }
        return hongYuRsp;
    }

    /**
     * 如果预约人身份证包含110
     **/
    public HongYuRsp f20A05(F20A05Req f20A05Req) {
        HongYuRsp hongYuRsp = new HongYuRsp();
        if (f20A05Req.getHphm().indexOf("110") >= 0) {
            hongYuRsp.setRetcode(HongYuConstants.FAILED);
            hongYuRsp.setRetmessage("[挡板]进行中不允许退办");
        } else {
            hongYuRsp.setRetcode(HongYuConstants.SUCCESS);
            hongYuRsp.setRetmessage("[挡板]退办成功");
        }
        return hongYuRsp;
    }

    /**
     * 如果车牌号包含W，则上传图片失败
     **/
    public HongYuRsp f20A06(F20A06Req f20A06Req) {
        HongYuRsp hongYuRsp = new HongYuRsp();
        if (f20A06Req.getHphm().indexOf("W") >= 0) {
            hongYuRsp.setRetcode(HongYuConstants.FAILED);
            hongYuRsp.setRetmessage("[挡板]上传图片失败");
        } else {
            hongYuRsp.setRetcode(HongYuConstants.SUCCESS);
            hongYuRsp.setRetmessage("[挡板]上传成功");
        }
        return hongYuRsp;
    }

    /**
     * 如果是西藏的车，查询不到
     * 实验、临时、无号牌、假号牌、挪用号牌 随机每次随机变数据
     * <p>
     * 仅小型汽车和教练车可以起流水，其中教练车起流水失败
     * 车牌号中带有Y的：车辆信息异常
     * 车牌号中带有X的：有违章
     * 到期时间：匹配M* 的为几个月到期，其他的均为十个月到期（例如：京A0M100表示下个月到期，京B3M489表示四个月后到期）
     * 临时入境汽车：颜色、品牌、型号、类型随机变化
     * 车牌号最后一位为核定载客数：若为字母则默认载客数5
     *
     * @param f20A01Req
     * @return
     */
    private String createVeInfo(F20A01Req f20A01Req) {
        F20A01Rsp json = new F20A01Rsp();
        json.setHpzl(f20A01Req.getHpzl());
        json.setHphm(f20A01Req.getHphm());
        json.setClsbdh("*" + f20A01Req.getClsbdh());
        /*如果是西藏的车，查询不到*/
        if (f20A01Req.getHphm().indexOf("藏") >= 0) {
            return null;
        }
        /*车身颜色*/
        json.setCsys(randomCsys());
        /*车辆品牌*/
        json.setClpp(randomClpp());
        /*车辆型号*/
        json.setClxh(VO.radomString(8));
        /*车辆类型*/
        json.setCllx(randomCllx(f20A01Req.getHpzl()));
        /*核定载客：车牌号最后一位数：若为字母或0则默认载客数5*/
        json.setHdzk(randomHdzk(f20A01Req.getHphm()));
        /*使用性质：倒数第二位，若不在配置里，默认A非营运*/
        json.setSyxz(randomSyxz(f20A01Req.getHphm()));
        /*车辆状态：倒数第三位，若不在配置里，默认A正常*/
        json.setZt(randomZt(f20A01Req.getHphm()));
        /*车辆类型*/
        json.setCllx(randomCllx(f20A01Req.getHpzl()));
        /*检验到期日期：匹配M* 的为几个月到期，若为0，则上个月到期，其他的均为7天后到期*/
        json.setYxqz(randomYxqz(f20A01Req.getHphm()));
        /*保险终止日期，默认等于检验到期日期*/
        json.setBxzzrq(json.getYxqz());
        /*车辆登记日期：匹配D* 的为几年前，若为0，则为当日,其他均默认10年前*/
        json.setCcdjrq(randomCcdjrq(f20A01Req.getHphm()));
        json.setZzl("2000");
        return JSONObject.toJSONString(json);
    }

    /**
     * 核定载客：车牌号最后一位数：若为字母或0则默认载客数5
     **/
    private String randomHdzk(String hphm) {
        /*车牌号最后一位为核定载客数：若为字母则默认载客数5*/
        String hdzk = hphm.substring(hphm.length() - 1);
        if (!VO.isNumber(hdzk) || "0".equals(hdzk)) {
            hdzk = "5";
        }
        return hdzk;
    }

    /**
     * 车辆状态：车牌号倒数第三位，若不在配置里，默认A
     * A 正常
     * B 转出
     * C 被盗抢
     * D 停驶
     * E 注销
     * G 违法未处理
     * H 海关监管
     * I 事故未处理
     * J 嫌疑车
     * K 查封
     * L 暂扣
     * M 强制注销
     * N 事故逃逸
     * O 锁定
     * P 达到报废标准公告牌证作废
     * Q 逾期未检验
     **/
    private String randomZt(String hphm) {
        /*车牌号最后一位为核定载客数：若为字母则默认载客数5*/
        String zt = hphm.substring(hphm.length() - 3, hphm.length() - 2);
        List<ThirdHongyuDictionary> thirdHongyuDictionaries = hongyuDictionaryLogic.queryList(HongYuConstants.DMLB_ZT);
        List<String> list = thirdHongyuDictionaries.stream().map(ThirdHongyuDictionary::getDmz).distinct().collect(Collectors.toList());
        if (list.contains(zt)) {
            return zt;
        }
        return "A";
    }

    /**
     * 使用性质：车牌号倒数第二位，若不在配置里，默认A
     * A 非营运
     * B 公路客运
     * C 公交客运
     * D 出租客运
     * E 旅游客运
     * F 货运
     * G 租赁
     * H 警用
     * I 消防
     * J 救护
     * K 工程救险
     * L 营转非
     * M 出租转非
     * N 教练
     * O 幼儿校车
     * P 小学生校车
     * Q 初中生校车
     * R 危化品运输
     * S 中小学生校车
     * T 预约出租客运
     * U 预约出租转非
     **/
    private String randomSyxz(String hphm) {
        /*车牌号最后一位为核定载客数：若为字母则默认载客数5*/
        String syxz = hphm.substring(hphm.length() - 2, hphm.length() - 1);
        List<ThirdHongyuDictionary> thirdHongyuDictionaries = hongyuDictionaryLogic.queryList(HongYuConstants.DMLB_SYXZ);
        List<String> list = thirdHongyuDictionaries.stream().map(ThirdHongyuDictionary::getDmz).distinct().collect(Collectors.toList());
        if (list.contains(syxz)) {
            return syxz;
        }
        return "A";
    }

    /**
     * 检验到期时间：匹配M* 的为几个月到期，若为0，则上个月到期，其他的均为7天后到期
     **/
    private String randomYxqz(String hphm) {
        // 到期时间：匹配M* 的为几个月到期，其他的均为7天后
        Long yxqz;
        if (hphm.contains("M0")) {
            yxqz = DateHandle.dayPlusMonth(-1);
        } else if (hphm.contains("M1")) {
            yxqz = DateHandle.dayPlusMonth(1);
        } else if (hphm.contains("M2")) {
            yxqz = DateHandle.dayPlusMonth(2);
        } else if (hphm.contains("M3")) {
            yxqz = DateHandle.dayPlusMonth(3);
        } else if (hphm.contains("M4")) {
            yxqz = DateHandle.dayPlusMonth(4);
        } else if (hphm.contains("M5")) {
            yxqz = DateHandle.dayPlusMonth(5);
        } else if (hphm.contains("M6")) {
            yxqz = DateHandle.dayPlusMonth(6);
        } else if (hphm.contains("M7")) {
            yxqz = DateHandle.dayPlusMonth(7);
        } else if (hphm.contains("M8")) {
            yxqz = DateHandle.dayPlusMonth(8);
        } else if (hphm.contains("M9")) {
            yxqz = DateHandle.dayPlusMonth(9);
        } else {
            yxqz = DateHandle.dayPlusDay(7);
        }
        return DateTime.getDateFormat(yxqz, FormatDate.FORMAT_DATE_STR);
    }

    /**
     * 车辆登记日期：匹配D* 的为几年前，若为0，则为当日,其他均默认10年前
     **/
    private String randomCcdjrq(String hphm) {
        // 到期时间：匹配D* 的为几个月到期，其他的均为十个月到期
        Long ccdjrq;
        if (hphm.contains("D0")) {
            ccdjrq = DateTime.getDate();
        } else if (hphm.contains("D1")) {
            ccdjrq = DateHandle.dayPlusYear(-1);
        } else if (hphm.contains("D2")) {
            ccdjrq = DateHandle.dayPlusYear(-2);
        } else if (hphm.contains("D3")) {
            ccdjrq = DateHandle.dayPlusYear(-3);
        } else if (hphm.contains("D4")) {
            ccdjrq = DateHandle.dayPlusYear(-4);
        } else if (hphm.contains("D5")) {
            ccdjrq = DateHandle.dayPlusYear(-5);
        } else if (hphm.contains("D6")) {
            ccdjrq = DateHandle.dayPlusYear(-6);
        } else if (hphm.contains("D7")) {
            ccdjrq = DateHandle.dayPlusYear(-7);
        } else if (hphm.contains("D8")) {
            ccdjrq = DateHandle.dayPlusYear(-8);
        } else if (hphm.contains("D9")) {
            ccdjrq = DateHandle.dayPlusYear(-9);
        } else {
            ccdjrq = DateHandle.dayPlusDay(-11);
        }
        return DateTime.getDateFormat(ccdjrq, FormatDate.FORMAT_DATE_STR);
    }

    /**
     * 车辆类型
     **/
    private String randomCllx(String hpzl) {
        String key;
        if ("15".equals(hpzl)) {
            key = "B";//挂车
        } else if ("01".equals(hpzl)) {
            key = "H";//货车
        } else if ("02".equals(hpzl)) {
            key = "K3";
        } else if ("13".equals(hpzl)) {
            key = "N";//三轮汽车
        } else if ("14".equals(hpzl)) {
            key = "T";//拖拉机
        } else if ("22".equals(hpzl)) {
            key = "Q";//牵引车
        } else if ("25".equals(hpzl)) {
            key = "J";//农用机械
        } else if ("99".equals(hpzl)) {
            key = "Z";//作业车
        } else if ("07".equals(hpzl) || "08".equals(hpzl) || "09".equals(hpzl) || "10".equals(hpzl) || "11".equals(hpzl) || "12".equals(hpzl) || "17".equals(hpzl) || "19".equals(hpzl) || "21".equals(hpzl) || "24".equals(hpzl)) {
            key = "M";//摩托车
        } else {
            key = "K";
        }
        List<ThirdHongyuDictionary> thirdHongyuDictionaries = hongyuDictionaryLogic.queryList(HongYuConstants.DMLB_CLLX);
        List<String> list = thirdHongyuDictionaries.stream().map(ThirdHongyuDictionary::getDmz).filter(s -> s.indexOf(key) == 0).distinct().collect(Collectors.toList());
        int idx = new Random().nextInt(list.size());
        return list.get(idx);
    }

    /**
     * 车身颜色
     **/
    private String randomCsys() {
        List<ThirdHongyuDictionary> thirdHongyuDictionaries = hongyuDictionaryLogic.queryList(HongYuConstants.DMLB_CSYS);
        List<String> list = thirdHongyuDictionaries.stream().map(ThirdHongyuDictionary::getDmz).distinct().collect(Collectors.toList());
        int idx = new Random().nextInt(list.size());
        return list.get(idx);
    }

    /**
     * 车辆品牌
     **/
    private String randomClpp() {
        String[] arr = new String[]{"长城", "中华", "红旗", "解放", "金杯", "江淮", "江铃", "哈飞", "东风", "昌河", "长安", "红岩", "陕汽", "福田", "春兰", "华菱", "锐雁", "中顺", "宝龙", "安凯", "华普", "奇瑞", "长丰", "永源", "吉林", "重汽", "荣威", "渝安", "广汽", "华泰", "金龙", "曙光", "北汽", "大宇", "吉奥", "北吉", "欧朗", "富康", "青年", "津汽", "海马", "中兴", "宇通", "黄海", "双环", "力帆", "上汽", "川汽", "开瑞", "威麟", "瑞麒", "荣威", "裕隆", "夏利", "福迪", "五菱", "金旅", "中通", "奥拓", "少林", "吉利", "东南", "华晨", "理念", "黑豹", "跃进", "启辰", "华北", "华翔", "天马", "宝骏", "飞碟", "九龙", "汇众", "金程", "陆风", "众泰", "万丰", "华利", "秦川", "野马", "奔腾", "亚星", "比亚迪", "全球鹰", "新大地", "松花江", "奔驰", "别克", "奥迪", "雷诺", "标致", "奥迪", "宝马", "本田", "三菱", "路虎", "世爵", "大众", "福特", "丰田", "现代", "英伦", "讴歌", "大迪", "悍马", "双龙", "捷豹", "光冈", "莲花", "铃木", "欧宝", "起亚", "日产", "萨博", "道奇", "林肯", "雪铁龙", "斯柯达", "斯巴鲁", "沃尔沃", "雪佛兰", "马自达", "菲亚特", "迈巴赫", "法拉利", "西雅特", "巴博斯", "劳恩斯", "雅科仕", "纳智捷", "劳伦士", "卡尔森", "路特斯", "保时捷", "布加迪", "帕加尼", "劳斯莱斯", "雷克萨斯", "凯迪拉克", "英菲尼迪", "兰博基尼", "克莱斯勒", "玛莎拉蒂", "科尼赛克", "阿斯顿马丁"};
        int idx = new Random().nextInt(arr.length);
        return arr[idx];
    }

}
